//
//  LBPlusMinusTool.m
//  loveBeen
//
//  Created by guofei on 2016/11/22.
//  Copyright © 2016年 loveBeen. All rights reserved.
//

#import "LBPlusMinusTool.h"

@implementation LBPlusMinusTool

static id _instance;
+ (instancetype)sharedPlusMinusTool{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.selectedProductsModel = [NSMutableArray array];
        self.payProductModel = [NSMutableArray array];
        self.delProductModel = [NSMutableArray array];
    }
    return self;
}
//小红点调用
-(void)setChange:(BOOL)change{
    _change = change;
    
    if([_delegate respondsToSelector:@selector(ChangeTabbarCount:)]){
        
        [_delegate ChangeTabbarCount:self];
    }
    
}

//计算总数
-(NSString *)allCount{
    
    NSArray *array = self.selectedProductsModel;
    
    __block NSInteger count = 0;
    
    [array enumerateObjectsUsingBlock:^(LBSuperMarketProducts *  _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
        
        count += model.totalCount;
    }];
    
    return [NSString stringWithFormat:@"%ld",count];
}


//用于判断是不是全选

-(BOOL)isAllSelected{
    
    
    __block int count = 0;
    [self.selectedProductsModel enumerateObjectsUsingBlock:^(LBSuperMarketProducts * _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
        if (model.selected) {
            count ++;
        }
    }];
    if (count == self.selectedProductsModel.count) {
        return YES;
    }
    else{
        return NO;
    }
}



//所选商品总价格

-(float)selectedProductPrice{
    
    __block float price = 0.00;
    
    [self.selectedProductsModel enumerateObjectsUsingBlock:^(LBSuperMarketProducts * _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
        if(model.selected)
        {
            price += model.totalCount * [(model.partner_price)floatValue];
        }
    }];
    return price;
    
}

-(void)fastUpdate:(id)Object{
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"refrash" object:Object];
}



-(BOOL)isSelected{
    
    __block BOOL isSelected = NO;
    __block BOOL once = NO;
    [self.selectedProductsModel enumerateObjectsUsingBlock:^(LBSuperMarketProducts * _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
        if(!once){
            if (model.selected) {
                once = YES;
                isSelected = YES;
            }
        }
    }];
    return isSelected;
}


//返回所选商品种类
-(NSInteger)kindsOfSelectedProduct{
    
    __block NSInteger count = 0;
    [self.selectedProductsModel enumerateObjectsUsingBlock:^(LBSuperMarketProducts * _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
        if(model.selected)
        {
            count++;
        }
    }
     ];
    return count;
}
@end
